import request from '@/utils/request'
const api = {
  uploadFile: 'file/upload',
  permissionList: '/sys/role/permission/selection/query',
  roleSelList: '/sys/role/selection/query',
  franchiseeSelList: '/franchisee/selection/query',
  repaymentPlanSelList: '/repaymentPlan/selection/query',
  merchantSelList: '/merchant/selection/query',
  custMagerSelList: '/custMager/selection/query',
  capitalSelList: '/capital/selection/query',
  netSignInfo: '/custContract/netSignInfo'
}

function gbkconvert (gbkStr, callback) {
  var script = document.createElement('script')
  script.id = 'gbkconvert'
  script.src = 'data:text/javascript;charset=utf-8,gbkconvertCb("' + gbkStr + '");'
  document.body.appendChild(script)
  window.gbkconvertCb = (res) => {
    callback && callback(res)
  }
}

/**
 * permissionList func
 * @param parameter
 * @returns {*}
 */
export function getPermissionList (parameter) {
  return request({
    url: api.permissionList,
    method: 'post',
    data: parameter
  })
}

/**
 * roleSelList func
 * @param parameter
 * @returns {*}
 */
export function getRoleSelList (parameter) {
  return request({
    url: api.roleSelList,
    method: 'post',
    data: parameter
  })
}

/**
 * franchiseeSelList func
 * @param parameter
 * @returns {*}
 */
export function getFranchiseeSelList (parameter) {
  return request({
    url: api.franchiseeSelList,
    method: 'post',
    data: parameter
  })
}

/**
 * repaymentPlanSelList func
 * @param parameter
 * @returns {*}
 */
export function getRepaymentPlanSelList (parameter) {
  return request({
    url: api.repaymentPlanSelList,
    method: 'post',
    data: parameter
  })
}

/**
 * merchantSelList func
 * @param parameter
 * @returns {*}
 */
export function getMerchantSelList (parameter) {
  return request({
    url: api.merchantSelList,
    method: 'post',
    data: parameter
  })
}

/**
 * custMagerSelList func
 * @param parameter
 * @returns {*}
 */
export function getCustMagerSelList (parameter) {
  return request({
    url: api.custMagerSelList,
    method: 'post',
    data: parameter
  })
}

/**
 * capitalSelList func
 * @param parameter
 * @returns {*}
 */
export function getCapitalSelList (parameter) {
  return request({
    url: api.capitalSelList,
    method: 'post',
    data: parameter
  })
}

/**
 * netSignInfo func
 * @param parameter
 * @returns {*}
 */
 export function getNetSignInfo (parameter) {
  return request({
    url: api.netSignInfo,
    method: 'post',
    data: parameter
  })
}

/**
 * uploadFile func
 * @param parameter
 * @returns {*}
 */
export function uploadFile (parameter) {
  return request({
    url: api.uploadFile,
    method: 'post',
    headers: {
      'Content-Type': 'application/json;charset=UTF-8'
    },
    data: parameter
  })
}

/**
 * uploadFile func
 * @param parameter
 * @returns {*}
 */
export function downloadFile (url, parameter) {
  return new Promise(async (resolve, reject) => {
    const res = await request({
      url,
      method: 'post',
      headers: {
        'Content-Type': 'application/json;charset=UTF-8',
        'isDownload': true
      },
      data: parameter
    })
    // 通过a标签打开新页面下载文件
    const a = document.createElement('a')
    // 构造一个blob对象来处理数据
    const blob = new Blob([res.data])
    if (res.headers['content-disposition']) {
      // 拿到用户上传的文件名
      let fileName = res.headers['content-disposition']
      // 截取=之后的字符串
      fileName = fileName.substring(fileName.lastIndexOf('='))
      fileName = fileName.substring(1)
      // 编码转换为中文
      gbkconvert(fileName, (res) => {
        fileName = res
        // URL.createObjectURL()会产生一个url字符串，可以像使用普通 URL 那样使用它，比如用在 img.src 上
        a.href = URL.createObjectURL(blob)
        // a标签里有download属性可以自定义文件名
        a.setAttribute(
          'download',
          fileName
        )
        document.body.appendChild(a)
        a.click()
        document.body.removeChild(a)
        resolve()
      })
    }
  })
}
